<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>学生成绩管理系统</title>
    <script src="/js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">
</head>
<body>

<!--失败，学生只能查看自己的个人信息-->
<div class="container-fluid" >
    <h2 align =center>学生个人信息</h2>
    <table class="table table-bordered table-striped table-hover" style="table-layout:fixed;" id="selfTable">
    </table>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                <form id="studentForm">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="sno">学号</label>
                        <input type="text" class="form-control" id="sno" name="sno" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                            <option value="3">未知</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄">
                    </div>
                </form>
                <!--表单结束-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>
            </div>
        </div>
    </div>
</div>

<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript">

    function saveStudent(){
        var data=$("#studentForm").serialize();
        var id=$("#id").val();
        if(id<1){
            //新增
            $.ajax({
                url:"/webapi/student/insert",
                method:"post",
                data:data
            }).done(function (){
                $('#selfTable').bootstrapTable("refresh");
                $('#myModal').modal('hide')
            })
        }
        else{
            //更新
            $.ajax({
                url:"/webapi/student/update",
                method:"put",
                data:data
            }).done(function (){
                $('#selfTable').bootstrapTable("refresh");
                $('#myModal').modal('hide')
            })
        }
    }

    //编辑
    function editStudent(id){
        $.ajax({
            url:'/webapi/student/get/'+id
        }).done(function (rs){
            $("#id").val(rs.id);
            $("#sno").val(rs.sno);
            $("#name").val(rs.name);
            $("#password").val(rs.password);
            $("#sex").val(rs.sex);
            $("#age").val(rs.age);
            $('#myModal').modal('show');
        })
    }


    function loadTable(){
        $('#selfTable').bootstrapTable({
            url:'/webapi/student/slist',
            columns:[{
                field:'id',
                title:'序号',
                align : 'center'
            },{
                field:'sno',
                title:'学号',
                align : 'center'
            },{
                field:'name',
                title:'姓名',
                align : 'center'
            },{
                field:'password',
                title:'密码',
                align : 'center'
            },{
                field:'sex',
                title:'性别',
                align : 'center',
                formatter: function(name) {
                    if(name==1){
                        return "男";
                    }else if(name==2){
                        return "女";
                    }else{
                        return "未知";
                    }
                }
            },{
                field:'age',
                title:'年龄',
                align : 'center'
            },{
                field: 'id',
                title:'操作',
                align : 'center',
                formatter:function (value,row,index){
                    var e='<a href="#" onclick="editStudent(\''
                        + row.id + '\')">编辑 </a>';
                    return e;
                }
            }]
        });
    }//end of loadTable()
    $(function(){
        loadTable();
        $('#myModal').modal('hide')
    })

</script>

</body>
</html>



